package cc.vv.baselibrary.view.columnchart;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Shader;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import cc.vv.baselibrary.R;
import cc.vv.baselibrary.util.ColorUtil;

/**
 * 自定义柱状图
 * Created by lh on 2017/11/6.
 */

public class GraphView extends View {
    /**
     * 柱形图顶部的颜色
     */
    private int leftColor = ColorUtil.setBCColor(R.color.color_28BFA9);
    /**
     * 柱形图底部的颜色
     */
    private int lefrColorBottom = ColorUtil.setBCColor(R.color.color_28BFA9);
    /**
     * 控件高度
     */
    private int mHeight;

    /**
     * 控件宽度
     */
    private int mWidth = 60;
    private Paint mChartPaint;
    /**
     * 时长
     */
    private float mValue;
    /**
     * 最大时长
     */
    private float mMaxTime = 100.0f;
    /**
     * 是否选中
     */
    private boolean mIsSelete;

    private int leftInt = 26;

    private int rightInt = 34;

    private int circleRadius = 5;

    private int pillarWidth = 8;

    public GraphView(Context context) {
        this(context, null);
    }

    public GraphView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public GraphView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    public void setDate(float value, boolean isSelete, float maxTime) {
        this.mValue = value;
        this.mIsSelete = isSelete;
        if (maxTime != 0) {
            this.mMaxTime = maxTime;
        }
        invalidate();
    }


    /**
     * 初始化画笔等
     */
    private void init() {
        mChartPaint = new Paint();
        mChartPaint.setAntiAlias(true);
    }


    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        mHeight = getHeight();
        mWidth = getWidth();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        //每格的高度
        double sizeHeight = (mHeight - dip2px(circleRadius+4)) / mMaxTime;
        mChartPaint.setStyle(Paint.Style.FILL);
//        leftInt = (mWidth-pillarWidth)/2;
//        rightInt = (mWidth-pillarWidth)/2+pillarWidth;
        //画柱状图
        RectF rectF = new RectF();
        rectF.left = dip2px(leftInt);
        rectF.right = dip2px(rightInt);
        rectF.bottom = mHeight;
        if (mValue != 0) {
            rectF.top = mHeight - (float) (mValue * sizeHeight)-1;
            if (mIsSelete) {
                LinearGradient lg = new LinearGradient(dip2px(leftInt), mHeight - (float) (mValue * sizeHeight), dip2px(leftInt), mHeight, leftColor, leftColor, Shader.TileMode.MIRROR);
                mChartPaint.setShader(lg);
            } else {
                LinearGradient lg = new LinearGradient(dip2px(leftInt), mHeight - (float) (mValue * sizeHeight), dip2px(leftInt), mHeight, leftColor, lefrColorBottom, Shader.TileMode.MIRROR);
                mChartPaint.setShader(lg);
            }
            mChartPaint.setColor(leftColor);
            canvas.drawRoundRect(rectF, 0, 0, mChartPaint);
            //画半圆
            RectF oval = new RectF(dip2px(leftInt), mHeight - (float) (mValue * sizeHeight) - dip2px(circleRadius), dip2px(rightInt), mHeight - (float) (mValue * sizeHeight) + dip2px(circleRadius));
            LinearGradient lg1 = new LinearGradient(dip2px(15), mHeight - (float) (mValue * sizeHeight) - dip2px(circleRadius),
                    dip2px(15), mHeight - (float) (mValue * sizeHeight), leftColor, leftColor, Shader.TileMode.MIRROR);
            mChartPaint.setShader(lg1);
            canvas.drawArc(oval, 180, 180, false, mChartPaint);
        }
    }


    /**
     * 根据手机分辨率转dp为px
     *
     * @param dpValue dp值
     * @return int
     */
    public int dip2px(float dpValue) {
        final float scale = getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }
}
